package com.epro.dx.activity.coupon.adapter;

import java.util.ArrayList;

import com.epro.dx.R;
import com.epro.dx.listenner.OnItemViewClickListenner;
import com.weipu.common.facade.model.CouponModel;
import com.weipu.common.util.Logger;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Tony
 *         couponMarket列表的数据适配器
 */
public class CouponMarketListAdapter extends BaseAdapter {

    private ArrayList<CouponModel> coupons;
    private Context mContext;

    private OnItemViewClickListenner viewClickListenner;

    public CouponMarketListAdapter(Context mContext, ArrayList<CouponModel> coupons) {
        this.mContext = mContext;
        this.coupons = coupons;

    }

    @Override
    public int getCount() {
        if (coupons != null) {
            return coupons.size();
        }
        return 0;
    }

    @Override
    public CouponModel getItem(int position) {
        return coupons.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setViewClickListenner(OnItemViewClickListenner listenner) {
        this.viewClickListenner = listenner;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_couponmarket_list, null);
            holder.couponInfo = (LinearLayout) convertView.findViewById(R.id.item_couponmarket_list_couponinfo);
            holder.getIt = (LinearLayout) convertView.findViewById(R.id.item_couponmarket_list_getit);
            holder.value = (TextView) convertView.findViewById(R.id.item_couponmarket_list_value);
            holder.need = (TextView) convertView.findViewById(R.id.item_couponmarket_list_need);
            holder.timeStart = (TextView) convertView.findViewById(R.id.item_couponmarket_list_validity_start);
            holder.timeEnd = (TextView) convertView.findViewById(R.id.item_couponmarket_list_validity_end);
            holder.desc = (TextView) convertView.findViewById(R.id.item_couponmarket_list_text_desc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CouponModel couponModel = coupons.get(position);
        //设置显示内容
        holder.value.setText(couponModel.getActionDes());
        holder.need.setText(Html.fromHtml(couponModel.getConditionDes()));
        holder.timeStart.setText(couponModel.getBeginDate() + "\n|");
        holder.timeEnd.setText(couponModel.getEndDate());
        holder.desc.setText(couponModel.getDescription());
        holder.getIt.setTag(position);
        holder.getIt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 点击调用接口领取Coupon
                if (null != viewClickListenner) {
                    viewClickListenner.onViewClick((Integer) v.getTag());
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        LinearLayout couponInfo;//coupon信息区域，用于点击进入相关的活动页面
        TextView value;//面值
        TextView need;//使用条件
        TextView timeStart;//有效期开始时间
        TextView timeEnd;//有效期截止时间
        TextView desc;//描述信息
        LinearLayout getIt;//点击获取的按钮
    }
}